﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace mamnontuoitho7.AppCode
{
    public class Rating
    {
        public ConnectGetData _connectData;
        public Rating()
        {
            _connectData = new ConnectGetData();
        }

        public bool RatingCreateOrRecreate(String productID, String memberID, String ratingMark)
        {
            try
            {
                SqlConnection con = _connectData.GetConnect();
                con.Open();

                string sqlUp = "select * from Rating where ProductID = @ProductID and MemberID = @MemberID";
                SqlCommand cmd = new SqlCommand(sqlUp, con);
                cmd.Parameters.AddWithValue("@ProductID", productID);
                cmd.Parameters.AddWithValue("@MemberID", memberID);

                DataTable dt = _connectData.GetDataTable(cmd);

                if (dt.Rows.Count > 0)// tức là ng này đã đánh giá rồi-> update điêm danh gia mới
                {
                    sqlUp = "update Rating set RatingMark = @RatingMark where ProductID = @ProductID and MemberID = @MemberID";
                    SqlCommand cmd3 = new SqlCommand(sqlUp, con);
                    cmd3.Parameters.AddWithValue("@ProductID", productID);
                    cmd3.Parameters.AddWithValue("@MemberID", memberID);
                    cmd3.Parameters.AddWithValue("@RatingMark", ratingMark);

                    cmd3.ExecuteNonQuery();
                    cmd3.Dispose();
                }
                else // tao mới dánh giá
                {
                    sqlUp = "Insert into Rating(ProductID, MemberID, RatingMark) values(@ProductID, @MemberID, @RatingMark)";
                    SqlCommand cmd2 = new SqlCommand(sqlUp, con);
                    cmd2.Parameters.AddWithValue("@ProductID", productID);
                    cmd2.Parameters.AddWithValue("@MemberID", memberID);
                    cmd2.Parameters.AddWithValue("@RatingMark", ratingMark);

                    cmd2.ExecuteNonQuery();
                    cmd2.Dispose();

                }
                con.Close();
            }
            catch (Exception ex)
            {

                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);
                return false;
            }
            return true;
        }

        public double AverageMark(String productID) 
        {

            try
            {
            SqlConnection con = _connectData.GetConnect();

            con.Open();

            string sql = "select AVG(Cast(RatingMark as float)) from Rating where ProductID = @ProductID";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("@ProductID", productID);

            object returnValue;

            returnValue = cmd.ExecuteScalar();

            double result = Math.Round(Convert.ToDouble(returnValue)*2) / 2.0f;
            return result;
            }
            catch (Exception ex)
            {

                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);
                return 0;
            }
            
        }
    }
}